package com.srs.action.user;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;

import com.srs.action.BaseAction;
import com.srs.dao.GroupDao;
import com.srs.dao.RGroupRoleDao;
import com.srs.dao.UserDao;
import com.srs.dao.cfg.RoleDao;
import com.srs.model.Group;
import com.srs.model.RGroupRole;
import com.srs.model.User;
import com.srs.model.cfg.Role;

public class UserAction extends BaseAction {

	private Integer page;
	private Integer pageNum;	
	private Integer start;
	private Integer end;
	private Integer total;	
	
	private User user;
	private List<User> users;
	
	private boolean loginFail = false;
	
	private String json;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Action user/list - User management
	 * 
	 * @return
	 */
	public String list() {
		System.out.println("UserAction::list");
		UserDao userDao = new UserDao();
		users = userDao.list();
		
		if (page==null){
			page = 1;
		}
		if (users.size()%RECORD_PER_PAGE==0){
			pageNum = users.size()/RECORD_PER_PAGE;
		} else {
			pageNum = users.size()/RECORD_PER_PAGE + 1;
		}
		
		start = (page-1)*RECORD_PER_PAGE + 1;
		total = users.size();
		if (total<(page)*RECORD_PER_PAGE){
			end = total;
		} else {
			end = (page)*RECORD_PER_PAGE;
		}
		
		List<User> pageUsers = new ArrayList<User>() ;
		GroupDao groupDao = new GroupDao();
		for(int i=0; i<users.size(); i++){
			if (i>=(page-1)*RECORD_PER_PAGE && i<(page)*RECORD_PER_PAGE){
				users.get(i).setIndex(i+1);
				try {
					users.get(i).setGroupName(groupDao.get(users.get(i).getGroupId()).getGroupName());
				} catch (Exception e) {
					users.get(i).setGroupName("Chưa thuộc group nào");
				}
				pageUsers.add(users.get(i));
			}
		}
		users = pageUsers;		
		return SUCCESS;
	}

	/**
	 * Action user/view - User management
	 * 
	 * @return
	 */
	public String view() {
		System.out.println("UserAction::view");
		UserDao userDao = new UserDao();
		GroupDao groupDao = new GroupDao();
		user = userDao.get(user.getUserId());
		try {
			user.setGroupName(groupDao.get(user.getGroupId()).getGroupName());
		} catch (Exception e) {
			user.setGroupName("Chưa thuộc group nào");
		}
		return SUCCESS;
	}

	/**
	 * Action user/add - User management
	 * 
	 * @return
	 */
	public String add() {
		System.out.println("UserAction::add");
		return SUCCESS;
	}

	/**
	 * Action user/addComplete - User management
	 * 
	 * @return
	 */
	public String addComplete() {
		System.out.println("UserAction::addComplete");
		UserDao userDao = new UserDao();
		userDao.insert(user);		
		return SUCCESS;
	}

	/**
	 * Action user/edit - User management
	 * 
	 * @return
	 */
	public String edit() {
		System.out.println("UserAction::edit");
		UserDao userDao = new UserDao();
		GroupDao groupDao = new GroupDao();
		user = userDao.get(user.getUserId());
		user.setGroupName(groupDao.get(user.getGroupId()).getGroupName());
		return SUCCESS;
	}

	/**
	 * Action user/editComplete - User management
	 * 
	 * @return
	 */
	public String editComplete() {
		System.out.println("UserAction::editComplete");
		UserDao userDao = new UserDao();
		userDao.update(user);
		return SUCCESS;
	}

	/**
	 * Action user/deleteComplete - User management
	 * 
	 */
	public String deleteComplete() {
		System.out.println("UserAction::deleteComplete");
		UserDao userDao = new UserDao();
		userDao.delete(user.getUserId());
		return SUCCESS;
	}

	public String login(){
		if (user==null){
			session.removeAttribute("loginUser");
			loginUser = null;
			return "login";
		}
		
		UserDao userDao = new UserDao();
		loginUser = userDao.login(user);
		if (loginUser!=null){
			GroupDao groupDao = new GroupDao();
			Group group = groupDao.get(loginUser.getGroupId());
			RGroupRoleDao rGroupRoleDao = new RGroupRoleDao();
			List<RGroupRole> rGroupRoles = rGroupRoleDao.list(group);
			
			RoleDao roleDao = new RoleDao();
			for (RGroupRole rGroupRole : rGroupRoles) {
				Role role = roleDao.get(rGroupRole.getRoleId());
				if (role.getRoleName().equalsIgnoreCase("ROLE_LOGIN")){
					loginUser.setRoleLogin(true);
				}
				if (role.getRoleName().equalsIgnoreCase("ROLE_USER_MANAGEMENT")){
					loginUser.setRoleUserManagement(true);
				}
				if (role.getRoleName().equalsIgnoreCase("ROLE_VIEW_GRAPHICAL_REPORTS")){
					loginUser.setRoleViewGraphicalReports(true);
				}
				if (role.getRoleName().equalsIgnoreCase("ROLE_RECEIVE_DAILY_REPORT")){
					loginUser.setRoleReceiveDailyReport(true);
				}
				if (role.getRoleName().equalsIgnoreCase("ROLE_RECEIVE_ALARM")){
					loginUser.setRoleReceiveAlarm(true);
				}
				if (role.getRoleName().equalsIgnoreCase("ROLE_AGENT_MANAGEMENT")){
					loginUser.setRoleAgentManagement(true);
				}
			}
			session.setAttribute("loginUser", loginUser);
			return SUCCESS;	
		}
		setLoginFail(true);
		session.removeAttribute("loginUser");
		return "login";
	}
	
	public String loginComplete(){
		UserDao userDao = new UserDao();
		User loginUser = userDao.login(user);
		return SUCCESS;
	}

	/**
	 * Action group/listJson - Role management
	 * 
	 */
	public String nameIsUsed() {
		System.out.println("RoleAction::listJson");

		// Init Dao
		UserDao userDao = new UserDao();
		
		// Put to json;
		setJson("{nameIsUsed: " +
				JSONArray.fromObject(userDao.nameIsUsed(user.getUserName())).toString() +
				"}");
		return SUCCESS;
	}		
/**********************************************************************************/
	
	/**
	 * @return the page
	 */
	public Integer getPage() {
		return page;
	}

	/**
	 * @param page the page to set
	 */
	public void setPage(Integer page) {
		this.page = page;
	}

	/**
	 * @return the pageNum
	 */
	public Integer getPageNum() {
		return pageNum;
	}

	/**
	 * @param pageNum the pageNum to set
	 */
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	/**
	 * @return the start
	 */
	public Integer getStart() {
		return start;
	}

	/**
	 * @param start the start to set
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * @return the end
	 */
	public Integer getEnd() {
		return end;
	}

	/**
	 * @param end the end to set
	 */
	public void setEnd(Integer end) {
		this.end = end;
	}

	/**
	 * @return the total
	 */
	public Integer getTotal() {
		return total;
	}

	/**
	 * @param total the total to set
	 */
	public void setTotal(Integer total) {
		this.total = total;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	/**
	 * @return the loginFail
	 */
	public boolean isLoginFail() {
		return loginFail;
	}

	/**
	 * @param loginFail the loginFail to set
	 */
	public void setLoginFail(boolean loginFail) {
		this.loginFail = loginFail;
	}

	/**
	 * @return the json
	 */
	public String getJson() {
		return json;
	}

	/**
	 * @param json the json to set
	 */
	public void setJson(String json) {
		this.json = json;
	}

	
}
